<template>
  <div id="login">
    <div class="head">
      <div class="text">在线学习平台</div>
      <div class="font">欢迎注册</div>
    </div>
    <div class="center">
      <div class="login_container">
        <div class="login">
          <div class="title">
            <span
              :class="{ active: loginType === 'sms' }"
              @click="changeLoginType('sms')"
              >欢迎注册</span
            >
          </div>

          <div class="login_form" >
            <el-form ref="form" :model="loginformUser" label-width="80px">
              <el-form-item>
                <el-input
                  v-model="loginformUser.username"
                  placeholder="请输用户名"
                  prefix-icon="el-icon-user-solid"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-input
                  type="password"
                  v-model="loginformUser.password"
                  placeholder="请输入密码"
                  prefix-icon="el-icon-lock"
                >
                </el-input>
              </el-form-item>

              <el-form-item>
                <el-input
                  type="text"
                  v-model="loginformUser.email"
                  placeholder="请输入邮箱"
      
                  prefix-icon="el-icon-s-promotion"
                >
                <template slot="append">
                  <el-button type="primary" size="small" @click="sendCode">{{
                    codeButtonText
                  }}</el-button>
                </template>
                </el-input>
                
              </el-form-item>
              <el-form-item>
                <el-input
                  type="text"
                  v-model="loginformUser.checkCode"
                  placeholder="请输入验证码"
                >
                </el-input>
              </el-form-item>
              <el-form-item>
                <el-checkbox v-model="agreement" style="color: #fff"
                  >我已阅读并同意</el-checkbox
                >
                <span>《用户协议》</span>
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  style="width: 100%"
                  @click="onSubmitother"
                  >注册</el-button
                >
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
   
  </div>
</template>
    
    <script>
import { onMounted } from "vue";
export default {
  name: "login",
  components: {},
  data() {
    return {
      loginformUser: {
        username: "",
        email: "",
        password: "",
        checkCode: "",
      },
      loginform: {
        number: "",
        code: "",
      },
      loginType: "sms", // 默认选中短信登录
      codeDisabled: false, // 是否禁用发送验证码按钮
      codeButtonText: "发送验证码", // 发送验证码按钮文本
      sendSuccess: false, // 是否发送成功
      agreement: false, // 是否同意用户协议
      isCodeShow: true, // 是否显示验证码登录
      isUserShow: false, // 是否显示密码登录

    };
  },
  methods: {
    // 登录按扭 密码登录
    onSubmitother() {
      if (this.agreement) {
        // 用户已同意用户协议，执行注册逻辑
        // 注册逻辑
        this.$http.post("/api/sysUser/register", this.loginformUser).then((res) => {
          console.log(res.data.data);
          if (res.data.code === 200) {
            //   获取token

            this.$message.success("注册成功");
              // 获取token
              localStorage.setItem("token", res.data.data.token);
              //记住密码
              localStorage.setItem("username", this.loginformUser.username);
              localStorage.setItem("password", this.loginformUser.password);
            this.$router.push("/complete");
          } else {
            this.$message.error(res.data.msg);
          }
        });
      } else {
        // 用户未同意用户协议，给出提示信息
        this.$message.error("请先同意用户协议");
      }
    },

    // 发送邮箱验证码
    sendCode() {
      //   检验是否有手机号
      if (!this.loginformUser.email) {
        this.$message.error("请输入邮箱");
        return;
      }
      this.codeDisabled = true; // 禁用按钮、
          this.sendSuccess = true; // 显示发送成功提示
          let seconds = 60;
          const timer = setInterval(() => {
            seconds--;
            this.codeButtonText = `${seconds}s后重新发送`;
            if (seconds === 0) {
              clearInterval(timer);
              this.codeDisabled = false; // 重新激活按钮
              this.codeButtonText = "发送验证码";
              this.sendSuccess = false; // 隐藏发送成功提示
            }
          }, 1000);
      let url = "/api/mail/send?email=" + this.loginformUser.email;
      this.$http.post(url).then((res) => {
        if (res.data.code === 200) {
          this.$message.success("发送成功");
        } else {
          this.$message.error("发送失败");
        }
      });
    },


   
  },
  mounted() {},
};
</script>
    
    
    
    
    
    <style lang="less" scoped>
#login {
  width: 100%;
  height: 100%;
  overflow-x: hidden;
  overflow: hidden;
  .head {
    height: 100px;
    width: 100%;
    .text {
      float: left;
      font-size: 35px;
      text-align: center;
      line-height: 100px;
      font-family: Arial, Helvetica, sans-serif;
      font-weight: bold;
      color: #3776da;
      text-align: left;
      padding-left: 200px;
    }
    .font {
      float: right;
      font-size: 18px;
      text-align: center;
      color: #5f5b5b;
      line-height: 100px;
      font-family: Arial, Helvetica, sans-serif;
      text-align: left;
      padding-right: 200px;
    }
  }

  .center {
    position: relative;
    height: 600px;
    width: 100%;
    background-size: cover;

    .login_container {
      position: absolute;
      width: 350px;
      height: 100%;
      left: 615px;
      top: 80px;
      .login {
        padding: 20px 20px 20px 20px;
        width: 280px;
        border-radius: 10px;
        box-shadow: 2;
        padding: 20px;
        width: 280px;

        background: rgb(
          255 255 255 / 4%
        ); /* 使用 rgba() 函数设置背景颜色和透明度 */
        border-radius: 10px;
        box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.2); /* 这里也可以使用 rgba() 函数设置阴影的颜色和透明度 */
        .title {
          width: 100%;
          height: 50px;
          span {
            margin-right: 30px;
            height: 50px;
            line-height: 50px;
            text-align: center;
            font-size: 18px;
            cursor: pointer;
          }
          span.active {
            color: #3776da;
          }
          span {
            &:hover {
              color: #3776da;
            }
          }
        }
      }
      .login_form {
        box-shadow: 2;
      }
    }
  }
  /deep/ .el-form-item__content {
    margin-left: 0 !important;
  }
  .footer {
    width: 100%;
    .intro {
      margin-left: 340px;
      width: 100%;
      ul {
        li {
          float: left;
          margin-left: 50px;
          color: #5f5b5b;
          font-size: 14px;
          cursor: pointer;
        }
        li {
          &:hover {
            color: #ff6700;
          }
        }
      }
    }
  }
}
/deep/ .el-checkbox__label {
  color: #000000;
}
</style>